package team.future.joke.crawler;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.spinn3r.log5j.Logger;

import cn.edu.hfut.dmic.webcollector.crawler.DeepCrawler;
import cn.edu.hfut.dmic.webcollector.model.Links;
import cn.edu.hfut.dmic.webcollector.model.Page;
import team.future.joke.constant.CollectorConstants;
import team.future.joke.constant.StatusConstants;
import team.future.joke.entity.Joke;
import team.future.joke.entity.JokeCollector;
import team.future.joke.param.JokeParam;
import team.future.joke.service.JokeCollectorService;
import team.future.joke.service.JokeService;
import team.future.joke.service.impl.SpringContextManager;

/**
 * Title: Haha365Crawler.java<br>
 * Description: <br>
 * Copyright (c) 未来工作室版权所有 2015    <br>
 * Create DateTime: 2015年9月1日 上午10:00:31 <br>
 * @author futurezhu
 */
public class Haha365Crawler extends DeepCrawler {
	
	public static final Logger logger = Logger.getLogger();
	
	public static final String SEED = "http://www.haha365.com/joke";
	public static final String SEED_INDEX = "http://www.haha365.com/joke/index_";
	public static int nid = 0;
	private JokeService jokeService = (JokeService) SpringContextManager.getBean("jokeService");
	private JokeCollectorService jokeCollectorService = (JokeCollectorService) SpringContextManager.getBean("jokeCollectorService");

	private JokeCollector jc = jokeCollectorService.getJokeCollector(CollectorConstants.PAGENAME_365);
	public Haha365Crawler() {
		super(CollectorConstants.BASE_FILE_PATH + File.separator + "365");
	}

	/**
	 * 程序入口
	 * @throws Exception
	 */
	public void run() throws Exception {
		this.addSeed(SEED);
		if (jc == null) {
			jc = new JokeCollector();
			jc.setFinshed(false);
			jc.setPageSize(0);
			jc.setWebName(CollectorConstants.PAGENAME_365);
			jokeCollectorService.insertJokeCollector(jc);
		}
		if (jc.isFinshed()) {
			//抽取过的每天抽10页
			this.start(10);
		} else {
			//设置默认每个网站第一次抽10页 
			this.start(10);
		}
	}

	  /*启动爬虫*/
	public static void main(String[] args) throws Exception {
		Haha365Crawler crawler = new Haha365Crawler();
		crawler.run();
	}

	public Links visitAndGetNextLinks(Page page) {
		dealWithPage(page);
		Element next = page.getDoc().select("p[id=pages]").select("a").last();
		if (next.text().equals("　下→页　")) {
			if (null != next.absUrl("href")) {
				Links links = new Links();
				links.add(next.absUrl("href"));
				logger.info(next.absUrl("href"));
				return links;
			}
		}
		String pageSizeStr = page.getUrl().substring(SEED_INDEX.length(), page.getUrl().length() - 4);
		int pageSize = Integer.parseInt(pageSizeStr);
		jc.setFinshed(true);
		jc.setPageSize(pageSize);
		jokeCollectorService.updateJokeCollector(jc);
		this.stop();
		return null;
	}

	private void dealWithPage(Page page) {
		//按题目显示分页... 去掉这段头
		page.getDoc().select("h3").select("a[class=catname]").remove();
		Elements titleDoc = page.getDoc().select("h3").select("A");
		Elements contentDoc = page.getDoc().select("div[id=endtext]");
		for (int i = 0; i < titleDoc.size(); i++) {
			if (jokeService.contain(titleDoc.get(i).text())) {
				continue;
			}
			Joke joke = new Joke();
			joke.setTitle(titleDoc.get(i).text());
			joke.setContent(contentDoc.get(i).text());
			joke.setType("0");
			joke.setStatus(StatusConstants.STATUS_JOKE_NORMAL);
			joke.setTime(new Date());
			joke.setNid(getNid());
			jokeService.insertJoke(joke);
		}
		
	}
	
	public synchronized int getNid(){
		if(nid==0){
			JokeParam param = new JokeParam();
			param.setPageIndex(0);
			param.setPageSize(1);
			List<Joke> list = jokeService.getJokeListPage(param);
			if(null!=list&&!list.isEmpty()){
				nid=list.get(0).getNid();
			}
		}
		return ++nid;
	}
}
